<?php

class Dinamico extends Base {


	/**
	 * Tabla a trabajar
	 *
	 * @param unknown_type
	 */
	public $tabla;
	/**
	 * Nombre del modulo
	 *
	 * @var unknown_type
	 */
	public $modulo;

	public function __construct($nucleo, $tabla){
		parent::__construct($nucleo);
		$this->tabla=$tabla;
		$this->modulo=get_class($this);
	}

	public function mostrarOpciones(){
		$this->usarDinamico();
		$this->smarty->assign("tabla", ucwords($this->tabla));
		$this->smarty->assign("clase", $this->modulo);
		$this->smarty->assign("ptl", "opciones.tpl");
	}

	public function verTodos(){
		$inicio=$this->par['inicio'];
		if($inicio==""){
			$inicio=0;
		}
		$orden=$this->par['orden'];
		
		$this->usarDinamico();
		$sql="SHOW COLUMNS FROM " . $this->tabla;
		$datos=$this->nucleo->conexion->Execute($sql);
		$numFilas = $datos->RecordCount();
		$numCampos=$numFilas;
		for ($i=0;$i<$numFilas;$i++){
			$x[$i]=($datos->fields["Field"]);
			if($datos->fields["Key"]=="PRI"){
				$llave=$datos->fields["Field"];
			}
			$xy[$i]=ucwords($datos->fields["Field"]);
			$campo[$i]=$datos->fields["Field"];
			$datos->MoveNext();
		}
		$this->smarty->assign("campos", $x);
		$this->smarty->assign("camposM", $xy);
		$this->smarty->assign("llave", $llave);

		if(trim($orden)!=""){
			$ordenx=strtolower($orden);
			$ordenx=" ORDER BY $orden  ASC";
		}
		$sql="SELECT * FROM " . $this->tabla . $ordenx . " LIMIT $inicio, 10";

		
		$datos=$this->nucleo->conexion->Execute($sql);
		$numFilas = $datos->RecordCount();
		for ($i=0;$i<$numFilas;$i++){

			for($j=0; $j<$numCampos; $j++){
				$z[$i][$campo[$j]]=$datos->fields[$campo[$j]];
			}

			$datos->MoveNext();

		}
		
		$sql="SELECT * FROM " . $this->tabla;
		$datos=$this->nucleo->conexion->Execute($sql);
		$numFilas = $datos->RecordCount();
		$pag=$numFilas/10;
		for ($i=0;$i<$pag;$i++){
			$pags[$i]["valor"]=10*$i;
			$pags[$i]["pagina"]=$i+1;
		}
		$this->smarty->assign("inicio", $inicio);
		$this->smarty->assign("fin", $inicio + 10);
		$this->smarty->assign("orden", $orden);
		
		$this->smarty->assign("paginas", $pags);
		$this->smarty->assign("campos", $x);
		$this->smarty->assign("datos", $z);
		$this->smarty->assign("clase", $this->modulo);
		$this->smarty->assign("ptl", "verTodos.tpl");
	}

	public function agregar(){
		$paso=$this->par['paso'];
		if($paso==""){
			$paso=1;
		}
		switch ($paso) {
			case 1:
				$this->usarDinamico();
				$sql="SHOW COLUMNS FROM " . $this->tabla;
				$datos=$this->nucleo->conexion->Execute($sql);
				$numFilas = $datos->RecordCount();
				$numCampos=$numFilas;
				for ($i=0;$i<$numFilas;$i++){
					$x[$i]["nombre"]=($datos->fields["Field"]);
					$tipo=$datos->fields["Type"];
					$tipo=explode("(", $tipo);
					if(count($tipo)==0){
						$tipo=$datos->fields["Type"];
					}else{
						$tam=explode(")",$tipo[1]);
						$tam=$tam[0];
						$tipo=$tipo[0];
						if($tipo=="enum"){
							$d=$datos->fields["Type"];
							$d=explode("(", $d);
							$d=$d[1];
							$d=explode(")", $d);
							$d=$d[0];
							$d=explode(",", $d);
							for($m=0; $m<count($d); $m++){
								$x[$i]["enum"][$m]=trim(str_replace("'","",$d[$m]));
							}
						}
					}
					$x[$i]["tipo"]=$tipo;
					$x[$i]["tam"]=$tam;
					$x[$i]["extra"]=$datos->fields["Extra"];
					if($datos->fields["Key"]=="PRI"){
						$llave=$datos->fields["Field"];
					}
					$xy[$i]=ucwords($datos->fields["Field"]);
					$campo[$i]=$datos->fields["Field"];
					$datos->MoveNext();
				}
				$this->smarty->assign("campos", $x);
				$this->smarty->assign("camposM", $xy);
				$this->smarty->assign("llave", $llave);
				$this->smarty->assign("titulo", "Agregar un " . ucwords($this->tabla));
				$this->smarty->assign("clase", $this->modulo);

				$this->smarty->assign("ptl", "agregar.tpl");
				break;
			case 2:
				$this->usarDinamico();
				$sql="SHOW COLUMNS FROM " . $this->tabla;
				$datos=$this->nucleo->conexion->Execute($sql);
				$numFilas = $datos->RecordCount();
				$numCampos=$numFilas;
				$obj=new $this->tabla();
				for ($i=0;$i<$numFilas;$i++){
					$campo=$datos->fields["Field"];
					if(strtolower($datos->fields["Null"])!="yes" && $datos->fields["Extra"]!="auto_increment"){
						if($this->par[$campo]=="" && $campo!="password"){
							$this->smarty->assign("msg", "Existen datos que no puede dejar vacios, error al agregar");
							$this->verTodos();
						}else{
							if($campo=="password"){
								if($this->par[$campo]!=""){
									$obj->$campo=md5(utf8_decode($this->par[$campo]));
								}
							}else{
								$obj->$campo=utf8_decode($this->par[$campo]);
							}
						}
					}else{
						$obj->$campo=utf8_decode($this->par[$campo]);
					}
					$datos->MoveNext();
				}
				if($obj->Save()){
					$this->smarty->assign("msg", $this->tabla . " agregado correctamente");
				}else{
					$this->smarty->assign("msg", "Error agregando " .$this->tabla );
				}
				$this->verTodos();
				break;
		}

	}

	public function editar(){
		$this->usarDinamico();
		$paso=$this->par['paso'];
		if($paso==""){
			$paso=1;
		}
		switch ($paso) {
			case 1:
				$this->usarDinamico();
				$sql="SHOW COLUMNS FROM " . $this->tabla;
				$datos=$this->nucleo->conexion->Execute($sql);
				$numFilas = $datos->RecordCount();
				$numCampos=$numFilas;

				for ($i=0;$i<$numFilas;$i++){
					$x[$i]["nombre"]=($datos->fields["Field"]);
					$tipo=$datos->fields["Type"];
					$tipo=explode("(", $tipo);
					if(count($tipo)==0){
						$tipo=$datos->fields["Type"];
					}else{
						$tam=explode(")",$tipo[1]);
						$tam=$tam[0];
						$tipo=$tipo[0];
						if($tipo=="enum"){
							$d=$datos->fields["Type"];
							$d=explode("(", $d);
							$d=$d[1];
							$d=explode(")", $d);
							$d=$d[0];
							$d=explode(",", $d);
							for($m=0; $m<count($d); $m++){
								$x[$i]["enum"][$m]=trim(str_replace("'","",$d[$m]));
							}
						}
					}
					$x[$i]["tipo"]=$tipo;
					$x[$i]["tam"]=$tam;
					$x[$i]["extra"]=$datos->fields["Extra"];
					if($datos->fields["Key"]=="PRI"){
						$llave=$datos->fields["Field"];
						$this->smarty->assign("llave", $llave);
					}
					$xy[$i]=ucwords($datos->fields["Field"]);
					$campo[$i]=$datos->fields["Field"];
					$datos->MoveNext();
				}
				$llave=$this->par['llave'];
				$nllave=$this->par['nllave'];
				$sql="SELECT * FROM " . $this->tabla . " WHERE $nllave='$llave'";
				$datos=$this->nucleo->conexion->Execute($sql);
				$numFilas = $datos->RecordCount();
				for ($i=0;$i<count($x);$i++){
					$x[$i]["valor"]=$datos->fields[$x[$i]["nombre"]];
				}
				$this->smarty->assign("campos", $x);
				$this->smarty->assign("camposM", $xy);

				$this->smarty->assign("titulo", "Editar un " . ucwords($this->tabla));
				$this->smarty->assign("clase", $this->modulo);
				$this->smarty->assign("ptl", "editar.tpl");
				break;
			case 2:
				$this->usarDinamico();
				$sql="SHOW COLUMNS FROM " . $this->tabla;
				$datos=$this->nucleo->conexion->Execute($sql);
				$numFilas = $datos->RecordCount();
				$numCampos=$numFilas;
				$llave=$this->par['llave'];
				$nllave=$this->par['nllave'];
				$obj=new $this->tabla();
				$obj->Load("$nllave='$llave'");
				for ($i=0;$i<$numFilas;$i++){
					$campo=$datos->fields["Field"];
					if(strtolower($datos->fields["Null"])!="yes" && $datos->fields["Extra"]!="auto_increment"){
						if($this->par[$campo]=="" && $campo!="password"){
							$this->smarty->assign("msg", "Existen datos que no puede dejar vacios, error editando");
							$this->verTodos();
							return;
						}else{
							if($campo=="password"){
								if($this->par[$campo]!=""){
									$obj->$campo=md5(utf8_decode($this->par[$campo]));
								}
							}else{
								//echo $campo . "=" . $this->par[$campo] . "<br />";
								$obj->$campo=utf8_decode($this->par[$campo]);
							}
						}
					}else{
						$obj->$campo=utf8_decode($this->par[$campo]);
					}

					$datos->MoveNext();
				}
				if($obj->Save()){
					$this->smarty->assign("msg", ucwords($this->tabla) . " editado correctamente");
				}else{
					$this->smarty->assign("msg", "Error editando " .ucwords($this->tabla) );
				}
				$this->verTodos();
				break;
		}

	}

	public function eliminar(){
		$this->usarDinamico();
		$paso=$this->par['paso'];
		if($paso==""){
			$paso=1;
		}
		switch ($paso) {
			case 1:
				$this->smarty->assign("llave", $this->par['llave']);
				$this->smarty->assign("nllave", $this->par['nllave']);
				$this->smarty->assign("titulo", "Eliminar un " . ucwords($this->tabla));
				$this->smarty->assign("clase", $this->modulo);
				$this->smarty->assign("ptl", "confEliminar.tpl");
				break;
			case 2:
				$llave=$this->par['llave'];
				$nllave=$this->par['nllave'];
				$obj=new $this->tabla();
				$obj->Load("$nllave='$llave'");
				if($obj->Delete()){
					$this->smarty->assign("msg", ucwords($this->tabla) . " eliminado correctamente");
					$this->verTodos();
				}else{
					$this->smarty->assign("msg", "Error intentando eliminar " . ucwords($this->tabla));
					$this->verTodos();
				}
				break;
		}
	}


	public function usarDinamico(){
		$this->smarty->assign("modulo", "dinamico");
	}
}
?>